import {
  IProjectDetailVo, ITaskExecutionRecordVo, IWorkflowExecutionRecordVo,
  IWorkflowVo, ITaskParamVo, ITriggerEventVo,
} from './type';
// todo: axios 实例,根据项目不同自行调整参数
import { restProxy } from './request';
 
/**
 * @description: api请求
 * 2022/3/22 16:47
 * @author: zsjin
 */
export const urls = {
  projectGroup: '/view/projects/groups',
  project: '/view/projects',
  // TODO 待改善，与/view/projects合并
  projectV2: '/view/v2/projects',
  workflow: '/view/workflow_instances',
  tasks: '/view/task_instances',
  task: '/view/task_instance',
  log: '/view/logs',
  dsl: '/view/workflow',
  processLog: '/view/logs/workflow',
  secretKey: '/view/namespaces',
  library: '/view/nodes',
  parameter: '/view/parameters',
  triggerEvent: '/view/trigger_events',
  trigger: '/view/trigger',
  version: 'https://jianmu.dev/versions/ci',
};

const addBaseUrl = (baseUrl: string) => baseUrl ? `${baseUrl}` : '';

/**
 * 获取项目详情
 * @param projectId
 * @param baseUrl
 */
export function fetchProjectDetail(
  projectId: string,
  baseUrl: string = '',
): Promise<IProjectDetailVo> {
  return restProxy({
    url: `${addBaseUrl(baseUrl)}${urls.project}/${projectId}`,
    method: 'get',
  });
}

/**
 * 获取任务执行记录列表
 * @param triggerId
 * @param baseUrl
 */
export function listTask(
  triggerId: string,
  baseUrl: string = '',
): Promise<ITaskExecutionRecordVo[]> {
  return restProxy<ITaskExecutionRecordVo[]>({
    url: `${addBaseUrl(baseUrl)}${urls.tasks}/${triggerId}`,
    method: 'get',
  });
}

/**
 * 获取dsl
 * @param workflowRef
 * @param workflowVersion
 * @param baseUrl
 */
export function fetchWorkflow(
  workflowRef: string,
  workflowVersion: string,
  baseUrl: string = '',
): Promise<IWorkflowVo> {
  return restProxy<IWorkflowVo>({
    url: `${addBaseUrl(baseUrl)}${urls.dsl}/${workflowRef}/${workflowVersion}`,
    method: 'get',
  });
}

/**
 * 获取流程执行记录列表
 * @param workflowRef
 * @param baseUrl
 */
export function listWorkflowExecutionRecord(
  workflowRef: string,
  baseUrl: string = '',
): Promise<IWorkflowExecutionRecordVo[]> {
  return restProxy<IWorkflowExecutionRecordVo[]>({
    url: `${addBaseUrl(baseUrl)}${urls.workflow}/${workflowRef}`,
    method: 'get',
  });
}

/**
 * 检查流程日志
 * @param processExecutionRecordId
 * @param baseUrl
 */
export function checkProcessLog(
  processExecutionRecordId: string,
  baseUrl: string = '',
): Promise<object> {
  return restProxy<object>({
    url: `${addBaseUrl(baseUrl)}${urls.processLog}/${processExecutionRecordId}`,
    method: 'head',
  });
}

/**
 * 获取流程日志
 * @param processExecutionRecordId
 * @param baseUrl
 */
export function fetchProcessLog(
  processExecutionRecordId: string,
  baseUrl: string = '',
): Promise<string> {
  return restProxy<string>({
    url: `${addBaseUrl(baseUrl)}${urls.processLog}/${processExecutionRecordId}`,
    method: 'get',
    timeout: 20 * 1000,
  });
}

/**
 * 检查日志
 * @param taskExecutionRecordId
 * @param baseUrl
 */
export function checkTaskLog(taskExecutionRecordId: string, baseUrl: string = ''): Promise<object> {
  return restProxy<object>({
    url: `${addBaseUrl(baseUrl)}${urls.log}/${taskExecutionRecordId}`,
    method: 'head',
  });
}

/**
 * 获取日志
 * @param taskExecutionRecordId
 * @param startRange
 * @param baseUrl
 */
export function fetchTaskLog(taskExecutionRecordId: string, baseUrl: string = '', startRange?: number ): Promise<string> {
  return restProxy<string>({
    url: `${addBaseUrl(baseUrl)}${urls.log}/${taskExecutionRecordId}`,
    method: 'get',
    headers: startRange === undefined ? undefined : {
      Range: `bytes=${startRange < 0 ? 0 : startRange}-`,
    },
    timeout: startRange === undefined ? 2 * 60 * 1000 : undefined,
  });
}

/**
 * 获取任务参数列表
 * @param taskId
 * @param baseUrl
 */
export function listTaskParam(taskId: string, baseUrl: string = ''): Promise<ITaskParamVo[]> {
  return restProxy<ITaskParamVo[]>({
    url: `${addBaseUrl(baseUrl)}${urls.task}/${taskId}/parameters`,
    method: 'get',
  });
}

/**
 * 获取触发器事件
 * @param triggerId
 * @param baseUrl
 */
export function fetchTriggerEvent(triggerId: string, baseUrl: string = ''): Promise<ITriggerEventVo> {
  return restProxy<ITriggerEventVo>({
    url: `${addBaseUrl(baseUrl)}${urls.triggerEvent}/${triggerId}`,
    method: 'get',
  });
}